#include <iostream>
#include <string>
#include <fstream>
#include "client.h"

Client::Client() : name(""), ip(""), is_first(true), file_path(""), login_num(0) {
   
}

bool Client::init() {
    name = "";
    ip = "";
    is_first = true;
    file_path = "";
    login_num = 0;
    return true;  
}


bool Client::set_info(const std::string& name, const std::string& ip) {
    if (name.empty() || ip.empty()) {
        std::cerr << "Name or IP cannot be empty" << std::endl;
        return false;
    }
    this->name = name;
    this->ip = ip;
    login_num++;
    if (is_first) {
        is_first = false; 
    }
    return true;
}

std::string Client::get_info() {
    return "Name: " + name + ", IP: " + ip + ", Login count: " + std::to_string(login_num);
}

bool Client::save(const std::string& file_path) { 
    if (name.empty()) {
        std::cerr << "No user data to save" << std::endl;
        return false;
    }
    std::ofstream file(file_path);
    if (!file.is_open()) {
        std::cerr << "Can't open file: " << file_path << std::endl;
        return false;
    }

    file << "[" <<tm.getCurrentDate() << "]\n"
         << "name:"<<name << "\n"
         << "ip:"<<ip << "\n"
         << "login time:"<<login_num << "\n";

    if (file.fail()) {
        std::cerr << "Save failed" << std::endl;
        return false; 
    }

    std::cout << "User data saved to: " << file_path << std::endl;
    return true;
}